package com.chenqq.qinghttp;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import com.chenqq.argument.HttpMethod;
import com.chenqq.configuration.QingHttp;
import com.chenqq.down.QingHttpDownTools;
import com.chenqq.field.Http_Down_Info;
import com.chenqq.model.DownInfo;
import com.chenqq.model.DownUpdate;
import com.chenqq.model.LogStrategy;
import com.chenqq.rxbus.Rxbus;

public class MainActivity extends AppCompatActivity implements Consumer<DownUpdate>{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QingHttp.getInstance().with(this.getApplication()) .setRetryCount(3) .setUrl("http://cq.sycdn.kuwo.cn/")
                .setFolder(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()) .setILogStrategy(new LogStrategy()) .init();
        QingHttpDownTools.getInStance().startDownService(this);
        QingHttp.getInstance().with(this.getApplication())
                .setRetryCount(3)//失败重复次数
                .setILogStrategy(new LogStrategy()).init();

    }

    @Override
    protected void onStart() {
        super.onStart();
        Disposable subscribe = BehaviorSubject.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Exception {
                Log.e("TAG==", "map-1:" + Thread.currentThread().getName());
                emitter.onNext(1);
            }
        })
                .subscribeOn(Schedulers.newThread())   .map(new Function<Integer, Integer>() {
            @Override
            public Integer apply(@NonNull Integer integer) throws Exception {
                Log.e("TAG==", "map-2:" + Thread.currentThread().getName());
                return 1;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function<Integer, Object>() {
                    @Override
                    public Object apply(@NonNull Integer integer) throws Exception {
                        Log.e("TAG==", "map-3:" + Thread.currentThread().getName());
                        return 2;
                    }
                }).doOnSubscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Log.e("TAG==", "map-3.5:" + Thread.currentThread().getName());
                    }
                })
                .observeOn(Schedulers.newThread())
                .map(new Function<Object, Object>() {
                    @Override
                    public Object apply(@NonNull Object o) throws Exception {
                        Log.e("TAG==", "map-4:" + Thread.currentThread().getName());
                        return 3;
                    }
                })
                .subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                Log.e("TAG==", "map-7:" + Thread.currentThread().getName());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.e("TAG==", "map-8:" + Thread.currentThread().getName());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                DownInfo downInfo=new DownInfo();
                downInfo.setMd5("");
                downInfo.setName("ce.mp4");
                downInfo.setUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
                QingHttpDownTools.getInStance().setmMethod(HttpMethod.GET)
                        .setRand(true)
                        .setNeedLog(true).downFileByTag("1112",downInfo);
            }
        },5000);
        Rxbus.getInStance().registerRxbus(Http_Down_Info.Down_Info,this,this);
    }

    @Override
    protected void onDestroy() {
        QingHttpDownTools.getInStance().stopDownTag("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        super.onDestroy();
    }

    @Override
    public void accept(DownUpdate downUpdate) throws Exception {

    }
}